package fr.cephb.joperon.core.db;



import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.db.DatabaseException;


import fr.cephb.berkeley.ByteReader;
import fr.cephb.berkeley.DBUtils;
import fr.cephb.joperon.core.Operon;
import fr.cephb.joperon.core.OperonDBSingleValue;
import fr.cephb.joperon.core.bio.BiologicalSequence;


public abstract class AbstractSequenceDB<T extends BiologicalSequence>
extends OperonDBSingleValue<String,T>
	{
	protected AbstractSequenceDB(Operon operon)
		{
		super(operon);
		}
	
	@Override
	public String readKey(DatabaseEntry key,DatabaseEntry data) throws DatabaseException {
		return new ByteReader(key).readCString();
		}
	
	@Override
	public String keyFromString(String s)
		{
		return s;
		}
	
	@Override
	protected DatabaseEntry keyToEntry(String key) throws DatabaseException
		{
		DatabaseEntry entry= new DatabaseEntry();
		entry.setData(DBUtils.string2CString(key));
		return entry;
		}
	
	/** get the length of the sequence or null if it does not exists */
	public Integer getSequenceSize(String key) throws DatabaseException
		{
		T seq= get(key);
		return(seq==null?null:seq.size());
		}
	
	/**
	 * return the subsequence of the key
	 * @param key the key
	 * @param start start offset 0 based
	 * @param length length of the subsequence
	 * @return the subsequence or null if it was not found
	 */
	public abstract T getSubSequence(String key,int start,int length) throws DatabaseException;
	
	protected abstract int convertByteLengthToSeqLength(int nBytes);
	}
